//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <winhack.h>
_ELASTOS_NAMESPACE_USING

#include <aura.h>
#include "consoleio.h"

EXTERN_C Boolean InitSrvconsole(PWChar caption, Int32 width, Int32 height);
EXTERN_C Void  QuitSrvconsole();

//Define global var about subprocess info
HANDLE g_hSubProcess        = NULL;                 //handle of subprocess
WChar  g_wszConsoleName[]    = L"ElEmuConsole.exe";  //name of subprocess

Boolean DestroyConsoleWindow()
{
    if (NULL != g_hSubProcess) {
        TerminateProcess(g_hSubProcess, 0);
        CloseHandle(g_hSubProcess);
    }

    return TRUE;
}

/* Create a subprocess with console window for elservices  */
Boolean CreateConsoleWindow(PWChar pwszTitle)
{
    STARTUPINFOW            pipe_si;
    PROCESS_INFORMATION     pipe_pi;
    Boolean                 bRes = FALSE;

    //Create subprocess with console window
    memset((PVoid)&pipe_si, 0, sizeof(STARTUPINFOW));
    memset((PVoid)&pipe_pi, 0, sizeof(PROCESS_INFORMATION));
    pipe_si.cb = sizeof(STARTUPINFOW);
    pipe_si.dwYCountChars = 5000;
    pipe_si.dwFlags |= STARTF_USECOUNTCHARS;
    bRes = CreateProcessW(NULL,
                          g_wszConsoleName,
                          NULL,
                          NULL,
                          TRUE,
                          CREATE_NEW_CONSOLE,
                          NULL,
                          NULL,
                          &pipe_si,
                          &pipe_pi);
    if (!bRes) {
        goto FailedExit;
    }
    g_hSubProcess = pipe_pi.hProcess;

    //Wait for creating subprocess
    HANDLE hConsoleEvent = 
                CreateEventW(NULL, FALSE, FALSE, L"StartedElServicesConsole");
    WaitForSingleObject(hConsoleEvent, (DWORD)INFINITE);
    CloseHandle(hConsoleEvent);

    //Free itself consoel window, in order to attach subprocess's console
    FreeConsole();

    //Attach subprocess's console window
    bRes = AttachConsole(pipe_pi.dwProcessId);
    if (!bRes) {
        goto FailedExit;
    }
    SetConsoleTitleW(pwszTitle);

    return TRUE;

FailedExit:
    DestroyConsoleWindow();
    return FALSE;
}

/* Init console for elservices */
Boolean InitSrvconsole(PWChar caption, Int32 width, Int32 height)
{
    HWND        hwndConsole;
    RECT        rect;
    Boolean     bRes = FALSE;

    bRes = GetConsoleConfigItem();
    if (!bRes) {
        goto FailedExit;
    }
    if (!g_bConsole) {
        return TRUE;
    }

    //Create console window and init stdou\stdin
    bRes = CreateConsoleWindow(caption);
    if (!bRes) {
        goto FailedExit;
    }

    bRes = OpenConsoleIo();
    if (!bRes) {
        goto FailedExit;
    }

    //Set attribute of console window
    //Get console window handle  
    hwndConsole = GetConsoleWindow();
    if (NULL == hwndConsole) {
        goto FailedExit;
    }
    bRes = GetWindowRect(hwndConsole , &rect);
    if (bRes) {
        MoveWindow(hwndConsole, rect.left, rect.top, width * 2, height, TRUE);
    }
    HMENU hmenu = GetSystemMenu(hwndConsole, FALSE);
    if (NULL != hmenu) {
        DeleteMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
        DrawMenuBar(hwndConsole);
        CloseHandle(hmenu);
    }
    CloseHandle(hwndConsole);

    //Init Ctrl_C handler
    SetConsoleCtrlHandler(NULL, TRUE);

    return TRUE;

FailedExit:
    QuitSrvconsole();
    return FALSE;
}

/* Deinit console */
Void QuitSrvconsole()
{
    CloseConsoleIo();
    DestroyConsoleWindow();
}
